<?php
/**
 * YICMS
 * ============================================================================
 * 版权所有 2014-2017 YICMS，并保留所有权利。
 * 网站地址: http://www.yicms.vip
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Created by PhpStorm.
 * Author: kenuo
 * Date: 2017/11/17
 * Time: 下午4:40
 */

namespace App\Repositories\PayAndConsume;


use App\Models\LogoutLog;
use App\Models\OnlineLog;
use App\Models\Stat;
use App\Models\PayLog;
use App\Models\LoginLog;
use App\Models\Retention;
use App\Models\Ltv;

class StatRepository
{
    /**
     * 获取全部的统计信息
     * @return mixed
     */
    public function getWithStat($uniqueID, $startTime, $endTime)
    {
        $gameID = session('game_id');

        return Stat::query()
            ->where('date', '>=', $startTime)
            ->where('date', '<=', $endTime)
            ->where(['game_id' => $gameID])
            ->whereIn('unique_id', $uniqueID)
            ->get();
    }

    /**
     * 获取全部的    留存率
     * @return mixed
     */
    public function getWithRentention($uniqueID, $startTime, $endTime)
    {
        $gameID = session('game_id');

        return Retention::query()
            ->where('date', '>=', $startTime)
            ->where('date', '<=', $endTime)
            ->where(['game_id' => $gameID])
            ->whereIn('unique_id', $uniqueID)
            ->get()->toArray();
    }

    /**
     * 获取全部的    Ltv
     * @return mixed
     */
    public function getWithLtv($uniqueID, $startTime, $endTime)
    {
        $gameID = session('game_id');

        return Ltv::query()
            ->where('date', '>=', $startTime)
            ->where('date', '<=', $endTime)
            ->where(['game_id' => $gameID])
            ->whereIn('unique_id', $uniqueID)
            ->get()->toArray();
    }

    //获取所有游戏的 各个渠道的 新增用户
    public function getChannelNewUsers($request, $channelsAuth)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' => [
                    'login_time' => [
                        '$gte' => $request['start_time'] . ' 00:00:00',
                        '$lte' => $request['end_time'] . ' 23:59:59'
                    ],
                    'register_time' => [
                        '$gte' => $request['start_time'] . ' 00:00:00',
                        '$lte' => $request['end_time'] . ' 23:59:59'
                    ],
                    'game_id' => $gameID,
                    'channel_id' => [
                        '$in' => $channelsAuth
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'id' => '$uid',
                        'channel_id' => '$channel_id',
                    ],
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => '$_id.channel_id',
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                    'new_users' => [
                        '$sum' => 1
                    ]
                ]
            ],
        ];


        $collection = LoginLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取所有游戏 各个渠道的 活跃用户
    public function getChannelActiveUsers($request, $channelsAuth)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' => [
                    'login_time' => [
                        '$gte' => $request['start_time'] . ' 00:00:00',
                        '$lte' => $request['end_time'] . ' 23:59:59'
                    ],
                    'game_id' => $gameID,
                    'channel_id' => [
                        '$in' => $channelsAuth
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'id' => '$uid',
                        'channel_id' => '$channel_id',
                    ],
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => '$_id.channel_id',
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                    'active_users' => [
                        '$sum' => 1
                    ]
                ]
            ],
        ];


        $collection = LoginLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取所有游戏 各个渠道的 收入
    public function getChannelTotalAmounts($request, $channelsAuth)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' => [
                    'pay_time' => [
                        '$gte' => $request['start_time'] . ' 00:00:00',
                        '$lte' => $request['end_time'] . ' 23:59:59'
                    ],
                    'game_id' => $gameID,
                    'channel_id' => [
                        '$in' => $channelsAuth
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => '$channel_id',
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                    'total_amounts' => [
                        '$sum' => [
                            '$toDouble' => '$amounts'
                        ]
                    ],
                ]
            ],
        ];


        $collection = PayLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取所有游戏 各个渠道的 活跃付费用户
    public function getChannelPayUsers($request, $channelsAuth)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' => [
                    'pay_time' => [
                        '$gte' => $request['start_time'] . ' 00:00:00',
                        '$lte' => $request['end_time'] . ' 23:59:59'
                    ],
                    'game_id' => $gameID,
                    'channel_id' => [
                        '$in' => $channelsAuth
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'id' => '$uid',
                        'channel_id' => '$channel_id',
                    ],
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => '$_id.channel_id',
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                    'pay_users' => [
                        '$sum' => 1
                    ]
                ]
            ],
        ];


        $collection = PayLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取游戏的 总支付次数   总支付用户数
    //    $total = $this->statRepository->getTotalPayUsersAndTotalPayTimes($request)->toArray();
    //    $totalPayTimes = isset($total[0]) ? $total[0]['total_pay_times'] : 0;
    //    $totalPayUsers = isset($total[0]) ? $total[0]['total_pay_users'] : 0;
    public function getTotalPayUsersAndTotalPayTimes($request)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' => [
                    'pay_time' => [
                        '$gte' => $request['start_time'] . ' 00:00:00',
                        '$lte' => $request['end_time'] . ' 23:59:59'
                    ],
                    'game_id' => [
                        '$eq' => $gameID
                    ],
                    'type' => [
                        '$in' => $request['type']
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'uid' => '$uid',
                    ],
                    'pay_times' => [
                        '$sum' => 1
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => 0,
                    'total_pay_users' => [
                        '$sum' => 1
                    ],
                    'total_pay_times' => [
                        '$sum' => '$pay_times'
                    ]
                ]
            ],
        ];


        $collection = PayLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取游戏的 在线人数
    public function getOnline($startTime, $endTime, $uniqueID)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' => [
                    'op_time' => [
                        '$gte' => $startTime . ' 00:00:00',
                        '$lte' => $endTime . ' 23:59:59'
                    ],
                    'game_id' => $gameID,
                    'unique_id' => [
                        '$in' => $uniqueID
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        '$substr' => ['$op_time', 0, 16]
                    ],
                    'total_amounts' => [
                        '$sum' => [
                            '$toDouble' => '$amounts'
                        ]
                    ],
                ]
            ],
            [
                '$sort' => [
                    '_id' => 1
                ]
            ]
        ];


        $collection = OnlineLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取游戏的 实时在线情况
    public function getLiveOnline($startTime, $endTime, $uniqueID)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' => [
                    'op_time' => [
                        '$gte' => $startTime . ' 00:00:00',
                        '$lte' => $endTime . ' 23:59:59'
                    ],
                    'game_id' => $gameID,
                    'unique_id' => [
                        '$in' => $uniqueID
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'register_time' => [
                            '$substr' => ['$op_time', 0, 16]
                        ]
                    ],
                    'sum_amounts' => [
                        '$sum' => [
                            '$toDouble' => '$amounts'
                        ]
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'register_time' => [
                            '$substr' => ['$_id.register_time', 0, 10]
                        ]
                    ],
                    'rush_hour' => [
                        '$max' => '$sum_amounts'
                    ],
                    'avg_amounts' => [
                        '$avg' => '$sum_amounts'
                    ],
                ]
            ],
            [
                '$sort' => [
                    '_id.register_time' => 1
                ]
            ],
            [
                '$project' => [
                    '_id' => 0,
                    'register_time' => '$_id.register_time',
                    'rush_hour' => '$rush_hour',
                    'avg_amounts' => [
                        '$trunc' => [
                            '$multiply' => ['$avg_amounts', 1]
                        ]
                    ],
                ]
            ]
        ];


        $collection = OnlineLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取 玩家等级分布
    public function getUserLevel($startTime, $endTime, $uniqueID)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' => [
                    'logout_time' => [
                        '$gte' => $startTime . ' 00:00:00',
                        '$lte' => $endTime . ' 23:59:59'
                    ],
                    'game_id' => $gameID,
                    'unique_id' => [
                        '$in' => $uniqueID
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'game_id' => '$game_id',
                        'unique_id' => '$unique_id',
                        'uid' => '$uid',
                    ],
                    'level' => [
                        '$last' => '$level'
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        '$toDouble' => '$level'
                    ],
                    'sum_active_users' => [
                        '$sum' => 1
                    ],
                ]
            ],
            [
                '$sort' => [
                    '_id' => 1
                ]
            ],
        ];


        $collection = LogoutLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }
}